<?php
namespace common\model;
use common\model\common_model;

/**
 * article模型数据库操作类
 */
class article_model extends common_model
{
    public $table_name = 'article';
    public $category = '';
    //已删除
    const STATUS_DELETED = -2;
    //关闭
    const STATUS_CLOSED  = -1;
    //正常
    const STATUS_YES      = 1;
    //草稿
    const STATUS_TEMP      = 2;


    public function __construct() {
        $this->db_config = load_config('database');
        $this->db_setting = 'default';
        parent::__construct();

    }

    /**
     * 获取数据库字段配置的常量数组
     * params:$getField设置的列
     * params $value 数值
     * return array || string
     * author:william
     * date:2019/01/17
     * */
    public static function getConstValue($getField, $value='all')
    {
        $m = [];
        if($getField == 'status'){
            $m[$getField] = [
                self::STATUS_DELETED     => '删除',
                self::STATUS_CLOSED      => '关闭',
                self::STATUS_YES         => '正常',
                self::STATUS_TEMP        => '草稿',
            ];
        }
        if( $value == 'all' ){
            return $m[$getField];
        }
        return isset($m[$getField][$value]) ? $m[$getField][$value] : [];
    }

    public function getList($filter=[],$fields='article_id,title,create_time',
                            $orderBy='sort_order DESC,article_id DESC',
                            $limit='0,10',$otherParams=[])
    {
        $date_format = $otherParams['date_format'];

        $where     = ["status>-2"];
        if(!empty($filter['title']))
        {
            $where[] = "title='{$filter['title']}'";
        }
        if(!empty($filter['article_id']))
        {
            $where[] = "article_id='{$filter['article_id']}'";
        }

        if(!empty($filter['cat_id']))
        {
            $where[] = $this->createIn('IN','cat_id',$filter['cat_id']);
        }
        if(!empty($filter['status']))
        {
            $where[] = $this->createIn('IN','status',$filter['status']);
        }
        $list = $this->select($where, $fields, $limit, $orderBy, $group = '', '');
        if(empty($list)){
            return $list;
        }

        foreach($list AS $k=>&$v)
        {
            //$otherParams['date_format'] => 'Ymd','Ymd H:i:s'
            $v['publish_date'] = date($date_format,$v['create_time']);
            if(isset($v['status'])){
                $v['sttus_name'] = $this->getConstValue('status',$v['status']);
            }
        }
        return $list;
    }

    //设置表名
    public function set_table($param=[]) {

        $this->table_name = $this->db_tablepre.$this->model[$param['modelid']]['tablename'];

    }

}
?>